package com.jason.bbs.block.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.jason.bbs.bean.block.Block;
import com.jason.bbs.block.dao.BlockDao;
import com.jason.bbs.block.service.BlockService;

@Service
public class BlockServiceImpl implements BlockService {
	@Resource
	private BlockDao blockDao;

	@Override
	public boolean exitis(String name) {
		Block block = new Block();
		block.setName(name);
		Example<Block> example = Example.of(block);
		return blockDao.exists(example);
	}

	@Override
	public Block add(String name) {
		Block block = new Block();
		block.setName(name);
		block.setCreateTime(new Date());
		block.setStatus(1);
		return blockDao.save(block);
	}

	@Override
	public void delete(String id) {
		Block block = blockDao.findById(Integer.valueOf(id)).get();
		block.setStatus(0);
		blockDao.save(block);
	}

	@Override
	public Page<Block> find(Block block, int size, int number) {
		if (block == null) {
			return blockDao.findAll(new PageRequest(number, size, Direction.DESC, "id"));
		}
		if (block.getStatus() == null) {
			block.setStatus(1);
		}
		return blockDao.findAll(Example.of(block), new PageRequest(number, size, Direction.DESC, "id"));
	}

	@Override
	public boolean exitisById(String id) {
		Block block=new Block();
		block.setId(Integer.valueOf(id));
		block.setStatus(1);
		return blockDao.exists(Example.of(block));
	}
}
